package qz.lixinyu.common.upload;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import qz.lixinyu.common.utils.R;
import qz.lixinyu.modules.company.entity.QyCompanyEntity;
import qz.lixinyu.modules.company.service.QyCompanyService;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("excel")
@Slf4j
public class ExcleExoprt {
    @Autowired
    private QyCompanyService qyCompanyService;

    /**
     * 批量导出
     */
//    @GetMapping("/export")
//    public void export(@RequestParam(required = false) String assetsName, @RequestParam(required = false) String key2,
//                       @RequestParam(required = false) String ids,
//                       HttpServletResponse response) throws IOException {
//        log.info("name{}",assetsName);  // 资产名
//        log.info("key{}",key2);   // 状态
//        ExcelWriter writer = ExcelUtil.getWriter(true);
//        List<QyCompanyEntity> list;
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        //导出勾选(条件加勾选)
//        if(StringUtils.isNotBlank(ids)){
//           /* String[] idsArr = ids.split(",");  //["1","2","3"]→ [1,2,3]*/
//            String[] id = ids.split(",");
//            Long[] idsArr= new Long[id.length];
//            for (int i = 0; i <id.length ; i++) {
//                idsArr[i]=Long.parseLong(id[i]);
//            }
//            queryWrapper.in("assets_id",idsArr);
//        }else{
//            // 第一种没有勾选默认全部导出(加条件)
//            queryWrapper.like(StringUtils.isNotBlank(assetsName),"assets_name",assetsName);
//            queryWrapper.eq(StringUtils.isNotBlank(key2),"assets_status",key2);
//        }
//        list = qyCompanyService.list(queryWrapper);
//        writer.write(list,true);
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("信息表"+".xlsx", "UTF-8"));
//        ServletOutputStream outputStream =response.getOutputStream();
//        writer.flush(outputStream,true);
//        outputStream.flush();
//        outputStream.close();
//    }
    /**
     * 导入Excel文件
     * @param
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    public R excelImport(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<QyCompanyEntity> qyCompanyEntities = reader.readAll(QyCompanyEntity.class);//reader.read(1, reader.getRowCount(), QyCompanyEntity.class);
        //System.out.println("进入这1XXXXXXXXXXXXXXXXXXX" + qyCompanyEntities);
        if (!CollectionUtil.isEmpty(qyCompanyEntities)) {
            for (QyCompanyEntity qyCompany : qyCompanyEntities) {
                String detail = qyCompany.getDetail();
                QueryWrapper<QyCompanyEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(detail), "detail", detail);
                List<QyCompanyEntity> list = qyCompanyService.list(queryWrapper);
                //System.out.println("查到的list" + list);

                if (!list.isEmpty()) {
                    continue;
                } else {
                    // 把职位状态设置为0（正常）  应聘总人数为0   录用人数默认为0   领域默认为互联网
                    qyCompany.setStatu(0);
                    qyCompany.setTotal(0);
                    qyCompany.setNum(0);
                    qyCompany.setCategorty("互联网");
                    qyCompany.setCreateTime(new Date());
                    //System.out.println("添加的数据为" + qyCompany);
                    qyCompanyService.save(qyCompany);
                }
            }
        }
        return R.ok();
    }



}
